package com.hitqz.robot.biz.flow.component.agv;

import cn.hutool.json.JSONObject;
import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
import com.hitqz.robot.biz.flow.domain.constant.FlowConstant;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2024/11/6
 */
@Slf4j
@LiteflowComponent(value = "ReadVarName", name = "[轮对/轮轴]读取数据变量等待返回")
@AllArgsConstructor
public class ReadVarName extends NodeComponent {

    private final RobotDriverService robotDriverService;

    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        String varName = this.getCmpData(JSONObject.class).get(FlowConstant.NODE_CUSTOM_INFO_MAP.get(this.getNodeId()).getParamNames().get(0), String.class).trim();
        String checkResult = this.getCmpData(JSONObject.class).get(FlowConstant.NODE_CUSTOM_INFO_MAP.get(this.getNodeId()).getParamNames().get(1), String.class);
        try {
            while (true) {
                LiteFlowUtil.checkInterruptFlag();
                R result = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.READ_VAR,
                        "{\"varName\":\"" + varName + "\",\"checkResult\":\"" + checkResult + "\"}");
                String readResult = (String) result.getData();
                if (checkResult.equals(readResult)) {
                    log.info("检查变量的值是{}--{} --{}", varName, checkResult, readResult);
                    break;
                }
                //log.info("检查变量的值是{}--{} --{}--{}", varName, checkResult, readResult, checkResult.equals(readResult));
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (Exception e) {
            log.info("读取变量异常" + e);
        }
    }

}
